package dq.study.concurrent.atomic;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author dongqun
 * @date 2020/1/16 15:05
 */
public class Test {

    private static Integer ii = 0;

    public static void main(String[] args) {
        AtomicInteger integer = new AtomicInteger();
        List<Thread> ts = new ArrayList<Thread>(600);
        long start = System.currentTimeMillis();
        for (int j = 0; j < 100; j++) {
            Thread t = new Thread(() -> {
                for (int i = 0; i < 10000; i++) {
                    unSafeCount();
                    integer.incrementAndGet();
                }
            });
            ts.add(t);
        }

        for (Thread thread : ts) {
            thread.start();
        }

        for (Thread t : ts) {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(ii);
        System.out.println(integer.get());
        System.out.println(System.currentTimeMillis() - start);
    }

    private static void unSafeCount() {
        ii++;
    }
}
